export function parseCycleRaw(hex: string): {
  isOn: boolean;
  repeatDays: number[];
} {
  if (hex.length < 4) return { isOn: false, repeatDays: [] };

  const switchByte = parseInt(hex.slice(0, 2), 16);
  const repeatByte = parseInt(hex.slice(2, 4), 16);

  const repeatDays: number[] = [];

  for (let i = 1; i <= 7; i++) {
    const bitMask = Math.pow(2, 7 - i); // 等价于 1 << (7 - i)
    const hasDay = Math.floor(repeatByte / bitMask) % 2 === 1;
    if (hasDay) {
      repeatDays.push(i);
    }
  }

  return {
    isOn: switchByte === 1,
    repeatDays,
  };
}

/**
 * 解析定时透传 Hex 指令
 */
export function parseTimingRaw(hex: string): {
  isOn: boolean;
  hour: number;
} {
  if (hex.length < 4) return { isOn: false, hour: 0 };
  const switchByte = parseInt(hex.slice(0, 2), 16);
  const hour = parseInt(hex.slice(2, 4), 16);
  return {
    isOn: switchByte === 1,
    hour,
  };
}
/**
 * 解析档位透传 Hex 指令
 */
export function parseGearRaw(hex: string): {
  isOn: boolean;
  gear: number;
} {
  if (hex.length < 4) return { isOn: false, gear: 0 };
  const switchByte = parseInt(hex.slice(0, 2), 16);
  const gear = parseInt(hex.slice(2, 4), 16);
  return {
    isOn: switchByte === 1,
    gear,
  };
}

/**
 * 解析区间透传 Hex 指令
 * @param hex 形如 "0108152030" 的十六进制字符串
 * @returns 解析后的对象
 */
export function parseSectionRaw(hex: string): {
  isOn: boolean;
  startHour: number;
  startMinute: number;
  endHour: number;
  endMinute: number;
} {
  if (hex.length !== 10)
    return { isOn: false, startHour: 0, startMinute: 0, endHour: 0, endMinute: 0 };

  const isOn = parseInt(hex.slice(0, 2), 16) === 1;
  const startHour = parseInt(hex.slice(2, 4), 16);
  const startMinute = parseInt(hex.slice(4, 6), 16);
  const endHour = parseInt(hex.slice(6, 8), 16);
  const endMinute = parseInt(hex.slice(8, 10), 16);

  return {
    isOn,
    startHour,
    startMinute,
    endHour,
    endMinute,
  };
}

/**
 * 解析场景透传 Hex 指令
 当前选择的场景：0：未选择  1-11 场景枚举
场景一：0: 自定义  1-11场景枚举
场景二：0: 自定义  1-11场景枚举
场景三：0: 自定义  1-11场景枚举
场景四：0: 自定义  1-11场景枚举
 */
export function parseSceneRaw(hex: string): {
  currentScene: number;
  scene1: number;
  scene2: number;
  scene3: number;
  scene4: number;
} {
  console.log('parseSceneRaw', hex);
  if (hex.length !== 10) return { currentScene: 0, scene1: 1, scene2: 2, scene3: 0, scene4: 0 };

  const currentScene = parseInt(hex.slice(0, 2), 16);
  const scene1 = parseInt(hex.slice(2, 4), 16);
  const scene2 = parseInt(hex.slice(4, 6), 16);
  const scene3 = parseInt(hex.slice(6, 8), 16);
  const scene4 = parseInt(hex.slice(8, 10), 16);

  return {
    currentScene,
    scene1,
    scene2,
    scene3,
    scene4,
  };
}

export function parseWorkRaw(hex: string): {
  startHour: number;
  startMinute: number;
  endHour: number;
  endMinute: number;
  days: number[];
  startTime: number;
  pauseTime: number;
} {
  if (hex.length < 18) {
    return {
      startHour: 0,
      startMinute: 0,
      endHour: 12,
      endMinute: 59,
      days: [],
      startTime: 10,
      pauseTime: 12,
    };
  }
  const startHour = parseInt(hex.slice(0, 2), 16);
  const startMinute = parseInt(hex.slice(2, 4), 16);
  const endHour = parseInt(hex.slice(4, 6), 16);
  const endMinute = parseInt(hex.slice(6, 8), 16);
  const repeatByte = parseInt(hex.slice(8, 10), 16);
  const repeatDays: number[] = [];
  for (let i = 0; i < 7; i++) {
    const bitMask = 1 << i; // bit0 表示周一，bit1 表示周二，依此类推
    const hasDay = (repeatByte & bitMask) !== 0;
    if (hasDay) {
      repeatDays.push(i + 1); // day = bit index + 1
    }
  }
  const startTime = parseInt(hex.slice(10, 14), 16);
  const pauseTime = parseInt(hex.slice(14, 18), 16);
  return {
    startHour,
    startMinute,
    endHour,
    endMinute,
    days: repeatDays,
    startTime,
    pauseTime,
  };
}
